//
//  CompanyDetairlController.m
//  Ten
//
//  Created by 韦显光 on 2018/8/26.
//  Copyright © 2018年 xg韦. All rights reserved.
//

#import "CompanyDetairlController.h"
#import "CompanyCell2.h"
#import "UIButton+WebCache.h"
#import "CompanyJieShaoCell.h"
@interface CompanyDetairlController ()
{
    NSArray*jobsary;
    NSDictionary*companyDic;
}
@property (weak, nonatomic) IBOutlet UITableView *tbv;
@property (weak, nonatomic) IBOutlet UIView *navback;
@property (weak, nonatomic) IBOutlet UIImageView *img;
@property (weak, nonatomic) IBOutlet WxgButton *imgbtno;
@property (weak, nonatomic) IBOutlet UILabel *lb1;
@property (weak, nonatomic) IBOutlet UILabel *lb2;
@property (weak, nonatomic) IBOutlet UILabel *lb3;
@property (weak, nonatomic) IBOutlet UILabel *lb4;
@property (weak, nonatomic) IBOutlet UIButton *btn1o;
@property (weak, nonatomic) IBOutlet UIButton *btn2o;
@property (weak, nonatomic) IBOutlet UIView *line;
@property (weak, nonatomic) IBOutlet UIButton *soucangBtno;

@end

@implementation CompanyDetairlController

- (void)viewDidLoad {
    [super viewDidLoad];
    _tbv.separatorColor=[UIColor clearColor];
    _btn1o.selected=YES;
    _navback.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0];
    if (@available(iOS 11.0, *)){
        _tbv.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        _tbv.contentInset = UIEdgeInsetsMake(0, 0, 49, 0);
    }else
    {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self loaddata];

}

-(void)loaddata
{
    [FuncPublic getData:self parameters:@{@"company_id":_Cid} RequestUrl:XcompanyDetail success:^(NSDictionary *acountInfo) {
        companyDic=acountInfo[@"company"];
        jobsary=acountInfo[@"jobs"];
        [_tbv reloadData];
        _lb1.text=WXGStr(companyDic[@"name"]);
        _lb2.text=WXGStr(companyDic[@"property"]);
        _lb3.text=[NSString stringWithFormat:@"公司规模【%@人】",companyDic[@"company_size"]];
        _lb4.text=WXGStr(companyDic[@"address"]);
        [_imgbtno sd_setImageWithURL:[companyDic[@"logo"] formatUrl] forState:UIControlStateNormal placeholderImage:WXGImage(@"zhanwei")];
        [[_imgbtno imageView]setContentMode:UIViewContentModeScaleAspectFit];
        [_img sd_setImageWithURL:[companyDic[@"background_img"] formatUrl] placeholderImage:WXGImage(@"zhanwei")];
        _soucangBtno.selected=[WXGStr(acountInfo[@"is_collect"]) isEqualToString:@"0"]?NO:YES;
    }];
}

- (IBAction)soucangBtna:(UIButton *)sender {
    
    [FuncPublic getData:self parameters:@{@"company_id":_Cid} RequestUrl:_soucangBtno.selected?CompanycancleCollect:CompanyaddCollect success:^(NSDictionary *acountInfo) {
        [WSProgressHUD showImage:nil status:@"收藏成功"];
        [self loaddata];
    }];

}

- (IBAction)changgeBtna:(UIButton *)sender {
      _btn1o.selected=NO;
      _btn2o.selected=NO;
      sender.selected=YES;
    CGPoint center = _line.center;
    center.x = sender.center.x;
    _line.center = center;
    [self loaddata];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark 返回每组行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _btn1o.selected?1:jobsary.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    if ( _btn1o.selected) {
        CompanyJieShaoCell*cell=[CompanyJieShaoCell CellWithTableView:tableView];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
     
        NSDictionary *options = @{ NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute :@(NSUTF8StringEncoding) };
        NSData *data = [companyDic[@"content"] dataUsingEncoding:NSUTF8StringEncoding];
         NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithData:data options:options documentAttributes:nil error:nil];
        //设置段落格式
        NSMutableParagraphStyle *para = [[NSMutableParagraphStyle alloc] init];
        para.lineSpacing = 3; para.paragraphSpacing = 5;
        [attStr addAttribute:NSParagraphStyleAttributeName value:para range:NSMakeRange(0, attStr.length)];
        cell.lb1.attributedText = attStr;
 
        return cell;
        
    }else
    {
        CompanyCell2*cell=[CompanyCell2 CellWithTableView:tableView];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell Cellinit:jobsary[indexPath.row]];
        return cell;
        
    }
  
}


#pragma mark 设置每行高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_btn1o.selected) {
        //计算html字符串高度
        NSMutableAttributedString *htmlString =[[NSMutableAttributedString alloc] initWithData:[companyDic[@"content"] dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute:[NSNumber numberWithInt:NSUTF8StringEncoding]} documentAttributes:NULL error:nil];
        
        [htmlString addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} range:NSMakeRange(0, htmlString.length)];
        
        CGSize textSize = [htmlString boundingRectWithSize:(CGSize){DEVW - 30, CGFLOAT_MAX} options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
        return textSize.height;
    }else
    {
        return 80;
    }
    
}
#pragma mark 点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{
    
}

@end
